package com.bwsoft.portal.action;

import com.bwsoft.admin.domain.Account;
import com.bwsoft.admin.domain.Advertisement;
import com.bwsoft.admin.domain.QualityControl;
import com.bwsoft.admin.domain.QualityControlJoiner;
import com.bwsoft.api.pojo.ResponsePojo;
import com.bwsoft.portal.domain.valueObject.QualityControlJoinerVO;
import com.bwsoft.portal.domain.valueObject.QualityControlListVO;
import com.bwsoft.portal.domain.valueObject.QualityControlVO;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class QualityControlAction extends PortalBaseAction {
	
	private static Logger LOG = Logger.getLogger(QualityControlAction.class);
	private List<HashMap> qcFieldStr;
	private List<HashMap> qcScopeStr;
	private List<HashMap> qcCountStr;
	private List<HashMap> qcNatureStr;
	private int pagecount = 0;
	private int pageSize = 0;
	private String s_field;
	private String s_scope;
	private String s_count;
	private String s_nature;
	private String s_qcName;
	private QualityControl qualityCtrl;
	private String projectName;
	private String field;
	private String nature;
	private String scope;
	private String count;
	private String standard;
	private Date beginDate;
	private String price;
	private Date endDate;
	private Date feedBackDate;
	private String gbjgxx;
	private String ptclsj;
	private String ptxz;
	private String description;
	private String other;
	private File img1;
	private String img1FileName;
	private File img2;
	private String img2FileName;
	private File img3;
	private String img3FileName;
	private Integer qualityId;
	private QualityControlVO qcVo;
	//显示类型，0：全部，1：我的质量控制，2：我发起的质量控制，3：我参与的质量控制
	private String showType;
	private Advertisement adLeft;
	private Advertisement adMid;
	private Advertisement adRight;
	
	public String index() {
		// 获取领域
		ResponsePojo responsePoj= dictionaryService.getData("qc_field");
		List<Map<String, Object>> returnMap = (List<Map<String, Object>>)responsePoj.getData();
		qcFieldStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		// 获取参加范围
		responsePoj= dictionaryService.getData("qc_scope");
		returnMap = (List<Map<String, Object>>)responsePoj.getData();
		qcScopeStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		// 获取参加人数
		responsePoj= dictionaryService.getData("qc_count");
		returnMap = (List<Map<String, Object>>)responsePoj.getData();
		qcCountStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		// 获取活动性质
		responsePoj= dictionaryService.getData("qc_nature");
		returnMap = (List<Map<String, Object>>)responsePoj.getData();
		qcNatureStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		long recordcount = 0;
		try {
			if (s_qcName != null && !"".equals(s_qcName)) {
				s_qcName = URLDecoder.decode(s_qcName, "UTF-8");
				s_qcName = URLDecoder.decode(s_qcName, "UTF-8");
			}
			Object sponsor = session.get("Account");
			recordcount = qualityControlService.getTotalCount(s_field, s_scope, s_count, s_nature, s_qcName, 
					showType, sponsor == null ? null : ((Account)sponsor).getAccountId());
		} catch (Exception e) {
			LOG.info("查找质量控制出错");
			//e.printStackTrace();
		}
		
		getPageInfo(recordcount);
		
		//获取广告
		adLeft = adService.getAdByPos(21);
		adMid = adService.getAdByPos(22);
		adRight = adService.getAdByPos(23);
				
		return "qualityIndex";
	}
	
	public String getQcJson() {
		List<QualityControl> qcList=null;
		List<QualityControlListVO> qcVOs = null;
		Object sponsor = session.get("Account");
			
		try {
			if (s_qcName != null && !"".equals(s_qcName)) {
				s_qcName = URLDecoder.decode(s_qcName, "UTF-8");
				s_qcName = URLDecoder.decode(s_qcName, "UTF-8");
			}
			qcList = qualityControlService.findData(page, rows, s_field, s_scope, s_count, s_nature, 
					s_qcName, showType, sponsor == null ? null : ((Account)sponsor).getAccountId());
			qcVOs = qualityControlService.getVo(qcList, sponsor == null ? null : ((Account)sponsor).getAccountId());
		} catch (Exception e) {
			LOG.info("查找质量控制出错");
			e.printStackTrace();
		}
		
		responseJson.put("rows", qcVOs);
		return "mapJsonResult";
	}
	
	public String showCreate() {
		// 获取领域
		ResponsePojo responsePoj= dictionaryService.getData("qc_field");
		List<Map<String, Object>> returnMap = (List<Map<String, Object>>)responsePoj.getData();
		qcFieldStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		// 获取参加范围
		responsePoj= dictionaryService.getData("qc_scope");
		returnMap = (List<Map<String, Object>>)responsePoj.getData();
		qcScopeStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		// 获取活动性质
		responsePoj= dictionaryService.getData("qc_nature");
		returnMap = (List<Map<String, Object>>)responsePoj.getData();
		qcNatureStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		// 获取参加人数
		responsePoj= dictionaryService.getData("qc_count");
		returnMap = (List<Map<String, Object>>)responsePoj.getData();
		qcCountStr = (List<HashMap>)returnMap.get(0).get("dicData");
		
		//获取广告
		adLeft = adService.getAdByPos(21);
		adMid = adService.getAdByPos(22);
		adRight = adService.getAdByPos(23);
				
		return "showCreate";
	}
	
	public String doCreate() {
		if (projectName == null || "".equals(projectName)) { 
			responseJson.put("result", 1);
			responseJson.put("msg", "项目不能为空！");
			return "mapJsonResult";
		}
		if (standard == null || "".equals(standard)) { 
			responseJson.put("result", 2);
			responseJson.put("msg", "检测标准不能为空！");
			return "mapJsonResult";
		}
		if (price == null || "".equals(price)) { 
			responseJson.put("result", 3);
			responseJson.put("msg", "价格不能为空！");
			return "mapJsonResult";
		}
		if (!isNumeric(price)) { 
			responseJson.put("result", 4);
			responseJson.put("msg", "价格必须为数字！");
			return "mapJsonResult";
		}
		if (beginDate == null) { 
			responseJson.put("result", 5);
			responseJson.put("msg", "开始时间不能为空！");
			return "mapJsonResult";
		}
		if (endDate == null) { 
			responseJson.put("result", 6);
			responseJson.put("msg", "结束时间不能为空！");
			return "mapJsonResult";
		}
		if (feedBackDate == null) { 
			responseJson.put("result", 7);
			responseJson.put("msg", "反馈时间不能为空！");
			return "mapJsonResult";
		}
		if (description == null || "".equals(price)) { 
			responseJson.put("result", 8);
			responseJson.put("msg", "项目描述不能为空！");
			return "mapJsonResult";
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		System.out.println();
		if(!beginDate.after(calendar.getTime())){
			responseJson.put("result", 5);
			responseJson.put("msg", "开始时间不能小于当前时间");
			return "mapJsonResult";
		}
		if(!endDate.after(beginDate)){
			responseJson.put("result", 6);
			responseJson.put("msg", "结束时间必须大于开始时间");
			return "mapJsonResult";
		}
		if(!feedBackDate.after(endDate)){
			responseJson.put("result", 7);
			responseJson.put("msg", "反馈时间必须大于结束时间");
			return "mapJsonResult";
		}
		String img1Type = "";
		if (img1FileName != null && !"".equals(img1FileName)) {
			img1Type = img1FileName.substring(img1FileName.lastIndexOf(".")).toLowerCase();
		}
		if(img1FileName != null && !"".equals(img1FileName) && !qualityControlService.isImage(img1Type)){
			responseJson.put("result", 9);
			responseJson.put("msg", "图1不是图片类型！");
			return "mapJsonResult";
		}
		if(img1FileName != null && !"".equals(img1FileName) && !newsService.isFitSize(img1)){
			responseJson.put("result", 10);
			responseJson.put("msg", "图1上传的图片不能超过8M！");
			return "mapJsonResult";
		}
		String img2Type = "";
		if (img2FileName != null && !"".equals(img2FileName)) {
			img2Type = img2FileName.substring(img2FileName.lastIndexOf(".")).toLowerCase();
		}
		if(img2FileName != null && !"".equals(img2FileName) && !qualityControlService.isImage(img2Type)){
			responseJson.put("result", 11);
			responseJson.put("msg", "图2不是图片类型！");
			return "mapJsonResult";
		}
		if(img2FileName != null && !"".equals(img2FileName) && !newsService.isFitSize(img2)){
			responseJson.put("result", 12);
			responseJson.put("msg", "图2上传的图片不能超过8M！");
			return "mapJsonResult";
		}
		String img3Type = "";
		if (img3FileName != null && !"".equals(img3FileName)) {
			img3Type = img3FileName.substring(img3FileName.lastIndexOf(".")).toLowerCase();
		}
		if(img3FileName != null && !"".equals(img3FileName) && !qualityControlService.isImage(img3Type)){
			responseJson.put("result", 13);
			responseJson.put("msg", "图3不是图片类型！");
			return "mapJsonResult";
		}
		if(img3FileName != null && !"".equals(img3FileName) && !newsService.isFitSize(img3)){
			responseJson.put("result", 14);
			responseJson.put("msg", "图3上传的图片不能超过8M！");
			return "mapJsonResult";
		}
		ResponsePojo rp = qualityControlService.saveQualityCtrl((Account)session.get("Account"), 
				projectName, field, scope, count,nature,price,standard,beginDate,feedBackDate,endDate,
				description,other,ptclsj,gbjgxx,ptxz,img1,img1FileName,img2,img2FileName,img3,img3FileName);
		//easyui返回的json的contentType必须为text/html
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		json.put("result", rp.getErrcode());
		json.put("msg", rp.getErrmsg());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public String contentShow() {
		try {
			QualityControl qualityCtrl = qualityControlService.findById(qualityId);
			if (qualityCtrl != null) {
				qcVo = qualityControlService.getVo(qualityCtrl);
			}
			
			//获取广告
			adLeft = adService.getAdByPos(21);
			adMid = adService.getAdByPos(22);
			adRight = adService.getAdByPos(23);
		} catch (Exception e) {
			LOG.info("获取质量控制出错");
			e.printStackTrace();
		}
		return "show";
	}
	
	public String myqc() {
		long recordcount = 0;
		try {
			if (s_qcName != null && !"".equals(s_qcName)) {
				s_qcName = URLDecoder.decode(s_qcName, "UTF-8");
				s_qcName = URLDecoder.decode(s_qcName, "UTF-8");
			}
			recordcount = qualityControlService.getTotalCount(s_field, s_scope, s_count, s_nature, s_qcName, 
					showType, ((Account)session.get("Account")).getAccountId());
		} catch (Exception e) {
			LOG.info("查找质量控制出错");
			e.printStackTrace();
		}
		
		getPageInfo(recordcount);
		return "myqc";
	}
	public String myqc1() {
		long recordcount = 0;
		try {
			if (s_qcName != null && !"".equals(s_qcName)) {
				s_qcName = URLDecoder.decode(s_qcName, "UTF-8");
				s_qcName = URLDecoder.decode(s_qcName, "UTF-8");
			}
			recordcount = qualityControlService.getTotalCount(s_field, s_scope, s_count, s_nature, s_qcName,
					showType, ((Account)session.get("Account")).getAccountId());
		} catch (Exception e) {
			LOG.info("查找质量控制出错");
			e.printStackTrace();
		}

		getPageInfo(recordcount);
		return "myqc1";
	}

	public String usercount() {
		long recordcount = 0;
		try {
			recordcount = qualityControlService.getUserCountTotalCount(qualityId);
		} catch (Exception e) {
			LOG.info("查找报名人员出错");
			e.printStackTrace();
		}
		getPageInfo(recordcount);
		return "usercount";
	}
	public String usercount1() {
		long recordcount = 0;
		try {
			recordcount = qualityControlService.getUserCountTotalCount(qualityId);
		} catch (Exception e) {
			LOG.info("查找报名人员出错");
			e.printStackTrace();
		}
		getPageInfo(recordcount);
		return "usercount1";
	}

	public String getUserCountJson() {
		List<QualityControlJoiner> qcList=null;
		List<QualityControlJoinerVO> qcVOs = null;
			
		try {
			qcList = qualityControlService.findUserCountData(page, rows, qualityId);
			qcVOs = qualityControlService.getJoinerVo(qcList);
		} catch (Exception e) {
			LOG.info("查找质量控制出错");
			e.printStackTrace();
		}
		
		responseJson.put("rows", qcVOs);
		return "mapJsonResult";
	}

	private void getPageInfo(long recordcount) {
		pagecount = (int)(recordcount % getRows() == 0 ? (recordcount / getRows()) : (recordcount / getRows() + 1));
		
		if (pagecount < 1)
		{
			pageSize = 1;
			pagecount = 1;
		}
		else if (pagecount > 8)
			pageSize = 8;
		else
			pageSize = pagecount;
	}
	
	public String signup() {
		ResponsePojo rp = qualityControlService.signup((Account)session.get("Account"), qualityId);
		responseJson.put("result", rp.getErrcode());
		responseJson.put("msg", rp.getErrmsg());
		return "mapJsonResult";
	}

	public List<HashMap> getQcFieldStr() {
		return qcFieldStr;
	}

	public void setQcFieldStr(List<HashMap> qcFieldStr) {
		this.qcFieldStr = qcFieldStr;
	}

	public List<HashMap> getQcScopeStr() {
		return qcScopeStr;
	}

	public void setQcScopeStr(List<HashMap> qcScopeStr) {
		this.qcScopeStr = qcScopeStr;
	}

	public List<HashMap> getQcCountStr() {
		return qcCountStr;
	}

	public void setQcCountStr(List<HashMap> qcCountStr) {
		this.qcCountStr = qcCountStr;
	}

	public List<HashMap> getQcNatureStr() {
		return qcNatureStr;
	}

	public void setQcNatureStr(List<HashMap> qcNatureStr) {
		this.qcNatureStr = qcNatureStr;
	}

	public int getPagecount() {
		return pagecount;
	}

	public void setPagecount(int pagecount) {
		this.pagecount = pagecount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getS_field() {
		return s_field;
	}

	public void setS_field(String s_field) {
		this.s_field = s_field;
	}

	public String getS_scope() {
		return s_scope;
	}

	public void setS_scope(String s_scope) {
		this.s_scope = s_scope;
	}

	public String getS_count() {
		return s_count;
	}

	public void setS_count(String s_count) {
		this.s_count = s_count;
	}

	public String getS_nature() {
		return s_nature;
	}

	public void setS_nature(String s_nature) {
		this.s_nature = s_nature;
	}

	public String getS_qcName() {
		return s_qcName;
	}

	public void setS_qcName(String s_qcName) {
		this.s_qcName = s_qcName;
	}

	public QualityControl getQualityCtrl() {
		return qualityCtrl;
	}

	public void setQualityCtrl(QualityControl qualityCtrl) {
		this.qualityCtrl = qualityCtrl;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public String getNature() {
		return nature;
	}

	public void setNature(String nature) {
		this.nature = nature;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getStandard() {
		return standard;
	}

	public void setStandard(String standard) {
		this.standard = standard;
	}

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Date getFeedBackDate() {
		return feedBackDate;
	}

	public void setFeedBackDate(Date feedBackDate) {
		this.feedBackDate = feedBackDate;
	}

	public String getGbjgxx() {
		return gbjgxx;
	}

	public void setGbjgxx(String gbjgxx) {
		this.gbjgxx = gbjgxx;
	}

	public String getPtclsj() {
		return ptclsj;
	}

	public void setPtclsj(String ptclsj) {
		this.ptclsj = ptclsj;
	}

	public String getPtxz() {
		return ptxz;
	}

	public void setPtxz(String ptxz) {
		this.ptxz = ptxz;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getOther() {
		return other;
	}

	public void setOther(String other) {
		this.other = other;
	}

	public File getImg1() {
		return img1;
	}

	public void setImg1(File img1) {
		this.img1 = img1;
	}

	public String getImg1FileName() {
		return img1FileName;
	}

	public void setImg1FileName(String img1FileName) {
		this.img1FileName = img1FileName;
	}

	public File getImg2() {
		return img2;
	}

	public void setImg2(File img2) {
		this.img2 = img2;
	}

	public String getImg2FileName() {
		return img2FileName;
	}

	public void setImg2FileName(String img2FileName) {
		this.img2FileName = img2FileName;
	}

	public File getImg3() {
		return img3;
	}

	public void setImg3(File img3) {
		this.img3 = img3;
	}

	public String getImg3FileName() {
		return img3FileName;
	}

	public void setImg3FileName(String img3FileName) {
		this.img3FileName = img3FileName;
	}
	
	public String getCount() {
		return count;
	}

	public void setCount(String count) {
		this.count = count;
	}

	public boolean isNumeric(String str){ 
	   Pattern pattern = Pattern.compile("[0-9]*"); 
	   Matcher isNum = pattern.matcher(str);
	   if( !isNum.matches() ){
	       return false; 
	   } 
	   return true; 
	}

	public Integer getQualityId() {
		return qualityId;
	}

	public void setQualityId(Integer qualityId) {
		this.qualityId = qualityId;
	}

	public QualityControlVO getQcVo() {
		return qcVo;
	}

	public void setQcVo(QualityControlVO qcVo) {
		this.qcVo = qcVo;
	}

	public String getShowType() {
		return showType;
	}

	public Advertisement getAdLeft() {
		return adLeft;
	}

	public void setAdLeft(Advertisement adLeft) {
		this.adLeft = adLeft;
	}

	public Advertisement getAdMid() {
		return adMid;
	}

	public void setAdMid(Advertisement adMid) {
		this.adMid = adMid;
	}

	public Advertisement getAdRight() {
		return adRight;
	}

	public void setAdRight(Advertisement adRight) {
		this.adRight = adRight;
	}

	public void setShowType(String showType) {
		this.showType = showType;
	}
}
